package com.elite.controller;

import com.elite.entity.User;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

@RestControllerAdvice

public class TestRestControllerAdvice {

	/**
	 * 全局的数据绑定方法
	 *
	 * @param binder
	 */
	@InitBinder
	private void globalBinder(WebDataBinder binder) {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		CustomDateEditor dateEditor = new CustomDateEditor(df, true);
		//注册
		binder.registerCustomEditor(Date.class, dateEditor);

	}

	/**
	 * 全局统一异常处理
	 */
	@ExceptionHandler(Exception.class)
	public User globalExceptionHandler(Exception e){
		User user = new User();
		user.setAge(18);
		user.setUserName("全局异常处理器");
		return user;
	}
}
